package com.portal.notification;

import java.awt.event.MouseEvent;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Properties;

/**
 * @author Alan
 */
public class Notify {

	/**
	 * Properties for sending mail.
	 */
	private Properties sessionProperties;
	/**
	 * Serial.
	 */
	private static final long serialVersionUID = 2724429052795666180L;
	/**
	 * Path of svnlook.
	 */
	private String svnLookPath;
	/**
	 * Max of lines for diff.
	 */
	private int postDiffMaxLines;
	/**
	 * Rev.
	 */
	private String REV_PROP;
	/**
	 * Repo.
	 */
	private String REPO_PATH;
	/**
	 * Auth of changes.
	 */
	private String auth;
	/**
	 * Log of svn.
	 */
	private String svnLog;
	/**
	 * Change.
	 */
	private String change;
	// private Logger log = Logger.getLogger(SvnHooks.class);
	/**
	 * Width the windows.
	 */
	private final int width = 200;
	/**
	 * Height the windows.
	 */
	private final int height = 80;

	/**
	 * Constructor.
	 * @param repoPath Path of repository.
	 * @param revProp Rev.
	 */
	public Notify(final String repoPath, final String revProp) {
		if (loadPropertie()) {
			REPO_PATH = repoPath;
			REV_PROP = revProp;
			// setUndecorated(false);
			postCommit();
		} else {
			System.exit(-1);
		}
	}


	/**
	 * PostCommit.
	 */
	private void postCommit() {
		String[] getAuthor = {svnLookPath, "author", REPO_PATH, "-r", REV_PROP };
		String[] getLog = {svnLookPath, "log", REPO_PATH, "-r", REV_PROP };
		String[] getChanges = {svnLookPath, "changed", REPO_PATH, "-r",
				REV_PROP };
		String[] getDiffs = {svnLookPath, "diff", REPO_PATH, "-r", REV_PROP };

		Program command = new Program();

		auth = command.run(getAuthor).replace(
				System.getProperty("line.separator"), "");
		svnLog = command.run(getLog);
		change = command.run(getChanges);
		String diff;
		diff = command.run(getDiffs);
		Message mensajeSerial = new Message();
		mensajeSerial.setUsuario(auth);
		mensajeSerial.setCambios(change);
		mensajeSerial.setLog(diff);
		Socket socket;
		try {
			socket = new Socket("localhost", Integer.valueOf(sessionProperties.get("servidor.port").toString()));
			ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
			out.writeObject(mensajeSerial);
			out.close();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * Load a existing propertie file into the same path of directory.
	 * @return True - if the properties has been loaded.
	 * False - if the properties has not been loaded.
	 */
	private Boolean loadPropertie() {
		try {
			FileInputStream inSession = new FileInputStream("session.properties");
			sessionProperties = new Properties();
			sessionProperties.load(inSession);
			inSession.close();
			svnLookPath = sessionProperties.getProperty("svn.lookPath", "svnlook");
			postDiffMaxLines = Integer.parseInt(sessionProperties.getProperty("svn.postcommit.DiffMaxLines", "100"));
		} catch (IOException e) {
			e.printStackTrace();
			return Boolean.FALSE;
		}
		return Boolean.TRUE;
	}

	/**
	 * Main.
	 * @param args Arguments.
	 */
	public static void main(final String[] args) {
		new Notify(args[0], args[1]);
	}

	public void mouseClicked(MouseEvent e) {
		System.exit(0);
	}

	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
	}

	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
	}

	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
	}

}
